import 'package:xbaistack/common/gen.dart';
import 'package:xbaistack/l10n/app_localizations.dart';
import 'package:xbaistack/page/select_langue.dart';
import 'package:xbaistack/page/setting_theme.dart';
import 'package:xbaistack/widget/my_button.dart';
import 'package:xbaistack/widget/state_view.dart';
import 'package:flutter/material.dart';

class MyPage extends StatefulWidget {
  const MyPage({super.key});

  @override
  State<MyPage> createState() => _MyPageState();
}

class _MyPageState extends State<MyPage> {
  late final StatusViewController _controller;

  @override
  void initState() {
    super.initState();
    _controller = StatusViewController();
    _loading(); // 初始化加载
  }

  /// 模拟的耗时操作
  /// 加载中显示2秒之后加载“首页”视图
  void _loading() {
    _controller.showLoading();
    Future.delayed(const Duration(seconds: 0), _controller.loaded);
  }

  @override
  Widget build(BuildContext context) {
    final String text = AppLocalizations.of(context).language;
    return StatusView(
      retry: _loading,
      controller: _controller,
      builder: (context) => Center(
        child: Column(
          children: [
            Text(
              context.l10n.language,
              style: TextStyle(color: context.theme.themeColor),
            ),
            Text(text),
            MyButtons(
              text: '设置语言',
              onTap: () {
                context.routeTo(SelectLanguePage);
              },
            ),
            MyButtons(
              text: '主题设置',
              type: ButtonType.WARN,
              onTap: () {
                context.routeTo(SettingThemePage);
              },
            ),
          ],
        ),
      ),
    );
  }
}
